package com.footballretriever.datasource;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.ArrayList;

import com.footballretriever.data.Player;
import com.footballretriever.data.PlayerSeason;

public class PlayerAICWebDAO extends PlayerWebDAO{

	public Player getPlayer(String id) {
		
		Player p = null;
		String name = "", birthdate = "", birthplace = "", height = "", weight = "", nation = "", role = "", playerId = id;
		int nameIndex = 2, birthdateIndex = 5, birthplaceIndex = 13, heightIndex = 21, weightIndex = 25, nationIndex = 17, roleIndex = 29;
		String testUrl1 = "http://aic.football.it/scheda/18283/nicco-gianluca.htm";
		String testUrl2 = "http://aic.football.it/scheda/23458/nwankwo-obiorah-emeka.htm";
		ArrayList<PlayerSeason> playerSeasonList = new ArrayList<PlayerSeason>();
		PlayerSeason playerSeason = new PlayerSeason(id);;
		
	 	try {
	 		
	 		
	 		URL url = new URL(testUrl2);
	 		URLConnection urlConnection = url.openConnection();
	 		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
	 		String inputLine;
	 		int index = 0, indexSeason = 0;
	 		boolean start = false, seasonLoopStart = false, seasonLoopBegin = false;
	 		
	 		while ((inputLine = bufferedReader.readLine()) != null) {
	 			
	 			if(inputLine.contains("Scheda anagrafica"))
	 				start = true;
	 			
	 			if(inputLine.contains("Scheda analitica della carriera")) {
	 				
	 				start = false;
	 				seasonLoopStart = true;
	 				
	 			}
	 			
	 			if(inputLine.contains("</table>") && seasonLoopStart)
	 				seasonLoopStart = false;
	 			
	 			if(start) {
		 			
	 				//System.out.println(inputLine);
	 				
	 				if(index == nameIndex)
	 					name = inputLine.split("anagrafici di")[1].trim().split("\"")[0].trim();
	 				
	 				else if(index == birthdateIndex)
	 					birthdate = inputLine.split(">")[1].trim().split("<")[0].trim();
	 				
	 				else if(index == birthplaceIndex)
	 					birthplace = inputLine.split(">")[1].trim().split("<")[0].trim();
	 				
	 				else if(index == nationIndex)
	 					nation = inputLine.split(">")[1].trim().split("<")[0].trim();
	 				
	 				else if(index == heightIndex)
	 					height = inputLine.split(">")[1].trim().split("<")[0].trim();
	 				
	 				else if(index == weightIndex)
	 					weight = inputLine.split(">")[1].trim().split("<")[0].trim();
	 				
	 				else if(index == roleIndex)
	 					role = inputLine.split(">")[1].trim().split("<")[0].trim();
	 				
	 				index++;
	 				
	 			}
	 			
	 			if(seasonLoopStart) {
	 				
	 				if(inputLine.contains("bg2") || inputLine.contains("bg1"))
	 					seasonLoopBegin = true;
	 				
	 				if(seasonLoopBegin) {
	 					
	 					System.out.println(inputLine);
	 					
	 					if(indexSeason == 1)
	 						playerSeason.setSeason(inputLine.split(">")[1].split("<")[0]);
	 					else if(indexSeason == 3)
	 						playerSeason.setChampionship(inputLine.split(">")[1].split("<")[0]);
	 					else if(indexSeason == 4) {
	 						if(inputLine.contains("href")) {
	 							
	 							playerSeason.setMatchPlayed(inputLine.split(">")[2].split("<")[0]);
		 						playerSeason.setMatchTotal(inputLine.split(">")[3].split("<")[0].replace("/", ""));
		 						
	 						}
	 						else {
	 							
	 							playerSeason.setMatchPlayed(inputLine.split(">")[1].split("/")[0]);
		 						playerSeason.setMatchTotal(inputLine.split(">")[1].split("/")[1].split("<")[0]);
	 							
	 						}
	 					}
	 					else if(indexSeason == 5) {
	 						
	 						playerSeason.setMinutePlayed(inputLine.split(">")[1].split("/")[0]);
	 						playerSeason.setMinuteTotal(inputLine.split(">")[1].split("/")[1].split("<")[0]);
	 						
	 					}
	 					else if(indexSeason == 6) {
	 						
	 						if(inputLine.contains("href")) {
	 							
	 							playerSeason.setGoal(inputLine.split(">")[2].split("<")[0]);
		 						
	 						}
	 						else {
	 							
	 							playerSeason.setGoal(inputLine.split(">")[1].split("<")[0]);
	 							
	 						}
	 						
	 					}
	 					else if(indexSeason == 7) {
	 						
	 						if(inputLine.contains("href")) {
	 							
	 							playerSeason.setSubstitutionDone(inputLine.split(">")[2].split("<")[0]);
		 						
	 						}
	 						else {
	 							
	 							playerSeason.setSubstitutionDone(inputLine.split(">")[1].split("<")[0]);
	 							
	 						}
	 						
	 					}
	 					else if(indexSeason == 8) {
		
	 						if(inputLine.contains("href")) {
	 							
	 							playerSeason.setSubstitutionHaved(inputLine.split(">")[2].split("<")[0]);
		 						
	 						}
	 						else {
	 							
	 							playerSeason.setSubstitutionHaved(inputLine.split(">")[1].split("<")[0]);
	 							
	 						}
		
	 					}
	 					else if(indexSeason == 9) {
	 						
	 						if(inputLine.contains("href")) {
	 							
	 							playerSeason.setYellowCard(inputLine.split(">")[2].split("<")[0]);
		 						
	 						}
	 						else {
	 							
	 							playerSeason.setYellowCard(inputLine.split(">")[1].split("<")[0]);
	 							
	 						}
		
	 					}
	 					else if(indexSeason == 10) {
	 						
	 						if(inputLine.contains("href")) {
	 							
	 							playerSeason.setRedCard(inputLine.split(">")[2].split("<")[0]);
		 						
	 						}
	 						else {
	 							
	 							playerSeason.setRedCard(inputLine.split(">")[1].split("<")[0]);
	 							
	 						}
		
	 					}
	 					else if(indexSeason == 11) {
	 						
	 						playerSeason.setDisqualification(inputLine.split(">")[1].split("<")[0]);
		
	 					}
	 					
	 					indexSeason++;
	 					
	 					if(indexSeason > 14) {
	 						seasonLoopBegin = false;
	 						playerSeasonList.add(playerSeason);
	 						playerSeason = new PlayerSeason(id);
	 						indexSeason = 0;
	 					}
	 					
	 				}
	 				
	 			}
	 			
	 		}
	 		
	 		bufferedReader.close();
	 		
	 	} catch (Exception e) {
	 		
         e.printStackTrace();
         
	 	}
	 	
	 	System.out.println("Name: -" + name + "-");
	 	System.out.println("Birthdate: -" + birthdate + "-");
	 	System.out.println("Birthplace: -" + birthplace + "-");
	 	System.out.println("Weight: -" + weight + "-");
	 	System.out.println("Height: -" + height + "-");
	 	System.out.println("Role: -" + role + "-");
	 	System.out.println("Nation: -" + nation + "-");
	 	
	 	for(PlayerSeason  ps: playerSeasonList)
	 		System.out.println(ps);
	 		
		return p;
		
	}

	public void SavePlayer(Player player) {
		Statement st;
		int status = -1;
		String query = " INSERT INTO player (id, name, birthdate, birthplace, nationality, height, weight, role) " +
				"VALUES ('" + player.getId() + "', '" + player.getName() + "', '" + player.getBirthdate() + "', '" + 
				player.getBirthplace() + "', '" + player.getNationality() + "', '" + player.getHeight() + "', '" + 
				player.getWeightNormal() + "', '" + player.getRole() + "');";
		try {
			
			Connection con = null;
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
	        con = DriverManager.getConnection("jdbc:odbc:AccessFarina","","");
			st = con.createStatement();
			status = st.executeUpdate(query);
			if(status!=1)
				System.out.println(status);
			if(status!=1)
				System.in.read();
			st.close();
			con.close();
			
		} catch (Exception e) {
			
			e.printStackTrace();
			
		} 
	}

}
